﻿#ifndef JCURVEWIDGET_P_H
#define JCURVEWIDGET_P_H

#include "../jcurvewidget.h"
#include "qwt_scale_draw.h"
#include "qwt_plot_picker.h"
#include "qwt_plot_marker.h"
#include "qwt_plot_zoomer.h"
#include "qwt_plot_legenditem.h"
#include "qwt_plot_curve.h"
#include "qwt_plot_directpainter.h"
#include "qwt_plot_shapeitem.h"
#include "qwt_date_scale_draw.h"

// - class JPlotDirectPainter -

class JPlotDirectPainter : public QwtPlotDirectPainter
{
public:
    explicit JPlotDirectPainter(QObject *parent = 0);
    virtual ~JPlotDirectPainter();

    virtual bool eventFilter(QObject *o, QEvent *e);

private:
};

// - class JTimeScaleDraw -

class JTimeScaleDraw : public QwtDateScaleDraw
{
public:
    explicit JTimeScaleDraw(Qt::TimeSpec timeSpec = Qt::LocalTime);

    void setFormat(const QString &format);

protected:
    virtual QwtText label(double) const;

private:
    QString q_format;
};

// - class JCanvasPicker -

class JCanvasPicker : public QObject
{
    Q_OBJECT
public:
    explicit JCanvasPicker(QwtPlot *plot);

    void clearSelect();

    const QwtPlotCurve *selectedCurve() const;

    bool isEnabled() const;

protected:
    virtual bool event(QEvent *e);
    virtual bool eventFilter(QObject *o, QEvent *e);

Q_SIGNALS:
    void selectedChanged(int index, JCurve *curve);

public Q_SLOTS:
    virtual void setEnabled(bool on);

private:
    bool select(const QPoint &pos);
    void release();
    void showCurve(bool showIt);
    void shiftPointCursor(bool up);
    void shiftCurveCursor(bool up);

    QwtPlot *plot();
    const QwtPlot *plot() const;

private:
    QwtPlotCurve *q_selectedCurve;
    int q_selectedPoint;
    bool q_isEnabled;
    bool q_mousePressed;
    bool q_mouseMoved;
};

// - class JTimePlotPicker -

class JTimePlotPicker : public QwtPlotPicker
{
public:
    explicit JTimePlotPicker(QWidget *canvas);
    explicit JTimePlotPicker(int xAxis, int yAxis, QWidget *canvas);
    explicit JTimePlotPicker(int xAxis, int yAxis,
                             RubberBand rubberBand, DisplayMode trackerMode, QWidget *canvas);

    bool isTimeXAxis() const;
    void setTimeXAxis(bool value);

protected:
    virtual QwtText trackerTextF(const QPointF &point) const;

private:
    bool d_isTimeXAxis;
};

// - class JLegendItem -

class JLegendItem: public QwtPlotLegendItem
{
public:
    JLegendItem();
};

// - class JPlotZoomer -

class JSiftAreaEditor;

class JPlotZoomer: public QwtPlotZoomer
{
    Q_OBJECT
public:
    JPlotZoomer(int xAxis, int yAxis, QWidget *canvas);
    virtual QwtText trackerTextF(const QPointF & point) const;

    JSiftAreaEditor *areaSift() const;

    bool isTimeXAxis() const;
    void setTimeXAxis(bool value);

public Q_SLOTS:
    void setZoomMode(bool enabled);
    void setSiftMode(bool enabled);

protected:
    bool accept(QPolygon &polygon) const;
    void begin();
    bool end(bool ok = true);

private:
    int d_currentShapeIndex;
    bool d_zoomModeEnabled;
    bool d_siftModeEnabled;
    bool d_isTimeXAxis;
    QPoint d_startPoint;
};

// - class JCavansOptionWidget -

class QCheckBox;
class QFormLayout;
class QComboBox;
class QPushButton;

class JCavansOptionWidget : public QWidget
{
    Q_OBJECT
public:
    explicit JCavansOptionWidget(QwtPlot *plot, QWidget *parent = 0);

    QwtPlot *plot();
    const QwtPlot *plot() const;

    bool isZoomMode() const;
    bool isSiftMode() const;
    bool isBaseLineVisible() const;
    bool isBaseLineEnabled() const;
    bool isCurveStyleVisible() const;
    bool isEditEnabled() const;
    bool isExportVisible() const;

    JCurveWidget::CurveStyle curveStyle() const;
    void setCurveStyle(JCurveWidget::CurveStyle curveStyle);

protected:
    void paintEvent(QPaintEvent *);

Q_SIGNALS:
    void zoomModeChanged(bool checked);
    void siftModeChanged(bool checked);
    void baseLineEnabledChanged(bool enabled);
    void antiChanged(bool enabled);
    void curveStyleVisibleChanged(bool visible);
    void curveStyleChanged(int curveStyle);
    void editEnabledChanged(bool enabled);
    void exportVisibleChanged(bool visible);
    void reset();
    void editClicked();

public Q_SLOTS:
    void setZoomMode(bool checked);
    void setSiftMode(bool checked);
    void setZoomModeVisible(bool visible);
    void setSiftModeVisible(bool visible);
    void setBaseLineVisible(bool visible);
    void setBaseLineEnabled(bool enabled);
    void setCurveStyleVisible(bool visible);
    void setEditEnabled(bool enabled);
    void setExportVisible(bool visible);

private:
    QwtPlot *d_plot;
    QCheckBox *d_checkBoxZoomMode;
    QCheckBox *d_checkBoxSiftMode;
    QCheckBox *d_checkBoxAnti;
    QFormLayout *q_formLayoutCurveStyle;
    QComboBox *q_comboBoxCurveStyle;
    QPushButton *q_buttonEdit;
    QCheckBox *d_checkBoxBaseLine;
    QPushButton *q_buttonExport;
    bool q_editEnabled;
};

// - class JDatabaseEdit -

class QTimeEdit;

class JDatabaseEdit : public QDialog
{
    Q_OBJECT
public:
    explicit JDatabaseEdit(QWidget *parent = 0);

    QTime minimumTime() const;
    void setMinimumTime(const QTime &time);

    QTime maximumTime() const;
    void setMaximumTime(const QTime &time);

    bool isRetain() const;
    void setRetain(bool retain);

Q_SIGNALS:
    void apply();

public Q_SLOTS:
    void setRange(const QTime &minimum, const QTime &maximum);

private:
    QTimeEdit *q_timeEditMin;
    QTimeEdit *q_timeEditMax;
    QCheckBox *q_checkBoxRetain;
    QPushButton *q_buttonApply;
};

// - class JCurveWidgetPrivate -

class JBaseLineShapeItem;

class JCurveWidgetPrivate
{
    J_DECLARE_PUBLIC(JCurveWidget)
    public:
        JCurveWidgetPrivate(JCurveWidget *parent) :
      q_ptr(parent),
        plot(0),
        optionWidget(0),
        databaseEdit(0),
        enableDbClick(false),
        showNormal(true),
        width(0),
        axisXYType(JCurveWidget::AxisXY_Invalid),
        marker(0),
        zoomer(0),
        canvasPicker(0),
        siftAreaEditor(0),
        legend(0),
        baseCurve(0)
    {

    }

    void init(bool zoom, const QString &title = QString::null);

private:
    QwtPlot *plot;
    JCavansOptionWidget *optionWidget;
    JDatabaseEdit *databaseEdit;

    bool enableDbClick;
    bool showNormal;
    int width;
    JCurveWidget::AxisXYType axisXYType;
    QwtPlotMarker *marker;
    JPlotZoomer *zoomer;
    JTimePlotPicker *picker;
    JCanvasPicker *canvasPicker;
    JSiftAreaEditor *siftAreaEditor;
    QwtPlotLegendItem *legend;
    JBaseLineShapeItem *baseCurve;    // 母线
    QList<QwtPlotShapeItem *> siftShapes;
    QVector<JCurve *> curves;
};

#endif // JCURVEWIDGET_P_H
